package connect4swt.gui;



import com.swtdesigner.*;
import game.Player;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.*;


import connect4swt.ClientApplication;



/**
 * GameTab class Class that represents a game tab element with the necessary widgets for a game. It should have been extended from TabItem, but SWT native widgets like TabItem do not allow subclassing. We could also have used CTabItem whick allows subclassing, but that's not a native widget.
 */

public class GameTab {

	public GameTab(TabFolder tabContainer, Player me, Player opponent, String gameName, int gameID) {
		this.gameID = gameID;
		this.userLogin = me.getLogin();
		this.opponentLogin = opponent.getLogin();
		
		tabItem = new TabItem(tabContainer, SWT.NONE);
		tabItem.setText("\"" + gameName + "\" :: " + opponentLogin);

		composite = new Composite(tabContainer, SWT.NONE);
		tabItem.setControl(composite);
		
		createContents();
		
		//The acess for choose the factory
		BoardFactory factory = BoardFactory.getFactory(gameName);
		gameBoard = factory.createBoard(this.gameCanvas, me, opponent, gameID);			
	}


	/**
	 * Initializes the game tab	
	 *
	 */
	private void createContents() {

		mainSashForm = new SashForm(composite, SWT.NONE);

		leftSashForm = new SashForm(mainSashForm, SWT.VERTICAL);

		txtChat = new Text(leftSashForm, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		txtChat.setBackground(SWTResourceManager.getColor(255, 255, 255));
		txtChat.setEditable(false);

		txtSendMsg = new Text(leftSashForm, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		txtSendMsg.addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent e) {
				if(e.character == '\r'){				
					if(!(getTxtSendMsg().getText().charAt(0) == '\r')) {
						ClientApplication.getController().getGUIMediator().sendChatMessage(opponentLogin,getTxtSendMsg().getText(), gameID);						
						getTxtChat().append(ClientApplication.getController().getUser().getLogin() + " says:\r\n");
						getTxtChat().append(getTxtSendMsg().getText() + "    \t\r\n");
						getTxtSendMsg().setText("");
					}
				}
			}
		});
		leftSashForm.setWeights(new int[] {370, 77 });

		gameCanvas = new Canvas(mainSashForm, SWT.NONE);

		mainSashForm.setBounds(0, 37, 786, 439);

		lblChat = new Label(composite, SWT.NONE);
		lblChat.setBounds(0, 8,77, 25);
		lblChat.setImage(SWTResourceManager.getImage(ClientWindow.class, CHAT_HEADER_IMAGE));

		lblGame = new Label(composite, SWT.NONE);
		lblGame.setImage(SWTResourceManager.getImage(ClientWindow.class, GAME_LABEL_IMAGE));
		lblGame.setBounds(361, 8, 83, 24);

		btnExit = new Button(composite, SWT.FLAT);
		btnExit.setImage(SWTResourceManager.getImage(ClientWindow.class, CLOSE_BUTTON_IMAGE));
		btnExit.setBounds(766, 0, 20, 20);
		btnExit.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent arg0) {
				if(gameBoard.isMatchOver())
					removeFromTabFolder();	
				else if(showQuitBox()){					
					ClientApplication.getController().getGUIMediator().sendGiveUpMessage(gameID);			
					removeFromTabFolder();
				}				
			}
		});

		mainSashForm.setWeights(new int[] {359, 426 });

		lblStatus = new Label(composite, SWT.NONE);
		lblStatus.setText("Status messages from this game...");
		lblStatus.setBounds(1, 480, 785, 13);

	}

	/**
	 * Method triggered when btnClose is pressed.
	 * Removes the current tab from the parent TabFolder.
	 */
	public void removeFromTabFolder() {
		tabItem.dispose();
	}


	/**
	 * If the user clicks the close button, a message box appears asking if the 
	 * user really wants to quit.
	 * @return
	 */
	public boolean showQuitBox(){
		if(this.gameBoard.isMatchOver()){
			return true;
		}

		if(!this.gameBoard.isMatchOver()){
			MessageBox msgBox = new MessageBox(this.gameCanvas.getShell(), SWT.ICON_WARNING | SWT.YES | SWT.NO);
			msgBox.setText("Warning");
			msgBox.setMessage("Do you really want to give up?");		
			if(msgBox.open() == SWT.YES) 
				return  true;			
			else
				return false;	
		}
		return true;
	}


	/**
	 * Show a message box containing the winner´s name
	 * 
	 * @param winner The winner´s name
	 */
	public void showWinner(String winner){
		MessageBox msgBox = new MessageBox(this.gameCanvas.getShell(), SWT.ICON_INFORMATION | SWT.OK);
		msgBox.setText("Match Over!");
		msgBox.setMessage("The match is Over! Winner is " + winner + "!");	
		msgBox.open();

		this.txtSendMsg.setEnabled(false);
	}

	/**
	 * If the match turns out to be a draw, a message box appears informing
	 * the user.
	 */
	public void showMatchIsTied(){
		MessageBox msgBox = new MessageBox(this.gameCanvas.getShell(), SWT.ICON_INFORMATION | SWT.OK);
		msgBox.setText("Match Over!");
		msgBox.setMessage("The match is Over!");	
		msgBox.open();

		this.txtSendMsg.setEnabled(false);
	}

	/**
	 * If a user forfeits a match, a message box appears informing the user.
	 *
	 */
	public void showGiveUp(){
		MessageBox msgBox = new MessageBox(this.gameCanvas.getShell(), SWT.ICON_INFORMATION | SWT.OK);
		msgBox.setText("Match Over!");
		msgBox.setMessage("The opponents forfeits. You win the match!");	
		msgBox.open();
	}


	/**
	 * @return
	 * @uml.property  name="txtSendMsg"
	 */
	public Text getTxtSendMsg() {
		return txtSendMsg;
	}

	/**
	 * @return
	 * @uml.property  name="txtChat"
	 */
	public Text getTxtChat() {
		return txtChat;
	}

	/**
	 * @return
	 * @uml.property  name="gameCanvas"
	 */
	public Canvas getGameCanvas() {
		return gameCanvas;
	}

	/**
	 * @return
	 * @uml.property  name="tabItem"
	 */
	public TabItem getTabItem() {
		return tabItem;
	}

	/**
	 * @return
	 * @uml.property  name="gameID"
	 */
	public long getGameID() {
		return gameID;
	}

	/**
	 * @return
	 * @uml.property  name="userLogin"
	 */
	public String getUserLogin() {
		return userLogin;
	}

	/**
	 * @return
	 * @uml.property  name="opponentLogin"
	 */
	public String getOpponentLogin() {
		return opponentLogin;
	}

	/**
	 * @return
	 * @uml.property  name="gameBoard"
	 */
	public Board getGameBoard() {
		return gameBoard;
	}

	public void setLblStatus(String text) {
		this.lblStatus.setText(text);
	}

	final String SEND_MESSAGE_BUTTON_IMAGE	= "/images/sendMsg.png";
	final String CHAT_HEADER_IMAGE 			= "/images/chatHeader.png";
	final String GAME_LABEL_IMAGE 			= "/images/gameHeader.png";
	final String CLOSE_BUTTON_IMAGE 		= "/images/close.png";

	private TabItem tabItem;
	private Composite composite;
	private SashForm mainSashForm;
	private SashForm leftSashForm;
	private Button btnExit;
	private Label lblChat;
	private Label lblGame;
	private Text txtChat;
	private Text txtSendMsg;
	private Canvas gameCanvas;
	private int gameID;
	private String userLogin;
	private String opponentLogin;
	public Board gameBoard;
	private Label lblStatus;
}
